package com.googlecode.lemyriapode.prgechecs.gui;

import com.googlecode.lemyriapode.prgechecs.univers.Domaine.Obstacle.TypeObstacle;
import com.googlecode.lemyriapode.prgechecs.univers.Univers;
import java.awt.Color;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;

import javax.swing.JLabel;
import javax.swing.JPanel;
import static com.googlecode.lemyriapode.prgechecs.univers.Domaine.*;


public class Board extends JPanel {

    public static JPanel ChessBoardPanel = new JPanel();
    public static JPanel MainPanel = new JPanel();
    public static JPanel actionpanel = new JPanel();
    public static JButton clear = new JButton();
    public static JButton modePosition = new JButton();
    private JLabel[][] JLabelEchiquier = new JLabel[maxX + 1][maxY + 1];
    public Color blanc = new Color(255, 255, 255);
    public Color noir = new Color(0, 0, 0);

    public Board(Univers U) {
        ChessBoardPanel.setLayout(new java.awt.GridLayout(maxX, maxY));
        ChessBoardPanel.setSize(320, 320);
        TypeObstacle[][] obstacles = U.domaine.obstacle.getListe();
        setJLabelObstacle(obstacles);
    }

    private void setJLabelObstacle(TypeObstacle[][] obstacles) {
        for (int co = 0; co < maxX; co++) {
            for (int ra = 0; ra < maxY; ra++) {
                setLabel(co, ra);
                JLabelEchiquier[co][ra].setIcon(getIcon(obstacles[maxX-co][ra+1]));
            }
        }
    }

    private void setLabel(int colonne, int rangee) {
        JLabelEchiquier[colonne][rangee] = new javax.swing.JLabel();
        JLabelEchiquier[colonne][rangee].setBackground(blanc);
        JLabelEchiquier[colonne][rangee].setText((maxX - colonne) + "," + (rangee + 1));
        JLabelEchiquier[colonne][rangee].setBorder(javax.swing.BorderFactory.createLineBorder(noir));
        JLabelEchiquier[colonne][rangee].setSize(40, 40);
        JLabelEchiquier[colonne][rangee].setOpaque(true);
        ChessBoardPanel.add(JLabelEchiquier[colonne][rangee]);
    }

    private Icon getIcon(TypeObstacle type) {
        if (type == null) {
            return null;
        }
        return new ImageIcon(getClass().getResource("/com/googlecode/lemyriapode/prgechecs/gui/pieces/" + type.getNom() + ".gif"));
    }
}
